package com.jackrain.nea.fc.dl.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.ViewReloadPurInOneCmd;
import com.jackrain.nea.fc.dl.mapper.WmsBackWriteMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;


/**
 * WMS回写，采购入库补偿
 * @author kangcong
 * @date 2018/08/21
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class ViewReloadPurInOneCmdImpl extends CommandAdapter implements ViewReloadPurInOneCmd {

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
//        log.debug("------------ViewReloadPurInOneCmdImpl---------------");
        ValueHolder vh = new ValueHolder();
        WmsBackWriteMapper wmsBackWriteMapper = ApplicationContextHandle.getBean(WmsBackWriteMapper.class);
        List<HashMap> listData = wmsBackWriteMapper.listPurInData1();
        JSONArray dataArray = listToJsonarray(listData);
        vh.put("data", dataArray);
        vh.put("code", 0);
        vh.put("message", "查询成功！");
        return vh;
    }

    /**
     * list TO jsonarra
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }
}
